package com.liu.my_blog_system.controller.admin;


import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.my_blog_system.entity.TbLooper;
import com.liu.my_blog_system.service.TbLooperService;
import com.liu.my_blog_system.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * tb_looper (TbLooper)表控制层
 *
 * @author liuchengjie
 * @since 2021-04-07 14:18:47
 */
@RestController
@RequestMapping("/admin/looper")
@Api(tags = "轮播图接口")
public class LooperController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private TbLooperService looperService;

    /**
     * 分页查询所有数据
     *
     * @param current
     * @param size
     * @return 所有数据
     */
    @GetMapping("/selectAll")
    @ApiOperation(value = "分页查询轮播图")
    public Result selectAll(@RequestParam("current") @NotBlank(message = "当前页不能为空") Long current,
                            @RequestParam("size") @NotBlank(message = "每页条数不能为空") Long size) {
        Result result = looperService.findLooperListPage(current,size);
        return result;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Result selectOne(@PathVariable("id") String id) {
        TbLooper looper = looperService.getById(id);
        return Result.ok().data("looper",looper);
    }

    /**
     * 新增数据
     *
     * @param looper 实体对象
     * @return 新增结果
     */
    @PostMapping
    public Result insert(@RequestBody TbLooper looper) {
        boolean save = looperService.save(looper);
        if (save) {
            return Result.ok().message("保存轮播图成功");
        }
        return Result.error().message("保存失败");
    }


    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public Result delete(@RequestParam("idList") List<Long> idList) {
        boolean b = looperService.removeByIds(idList);
        if (b) {
            return Result.ok().message("删除轮播图成功");
        }
        return Result.error().message("删除失败");
    }
}
